package com.example.hp.week12;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

public class SensorTestActivity extends AppCompatActivity implements SensorEventListener{

    //传感器输出信息的编辑框
    EditText gravityEdt, lightEdt;
    //定义传感器管理对象
    private SensorManager sensorManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor_test);

        //获取重力传感器输出信息的编辑框
        gravityEdt = (EditText) findViewById(R.id.gravityEdt);
        //获取光线传感器输出信息的编辑框
        lightEdt = (EditText) findViewById(R.id.lightEdt);
        //获取传感器管理 SensorManager 对象
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    }

    //重写 Activity 的 onResume 方法，在该方法中注册传感器监听事件
    @Override
    protected void onResume() {
        super.onResume();
        //为重力传感器注册监听器
        sensorManager.registerListener((SensorEventListener) this,
                sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),
                SensorManager.SENSOR_DELAY_GAME);
        //为光线传感器注册监听器
        sensorManager.registerListener((SensorEventListener) this,
                sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT),
                SensorManager.SENSOR_DELAY_GAME);
    }

    //重写 Activity 的 onPause 方法，在该方法中注销传感器事件
    // 在页面推失去焦点时取消注册的监听器，使之不再起作用（否则就是捣乱了）
    @Override
    protected void onPause() {
        sensorManager.unregisterListener((SensorEventListener) this);
        super.onPause();
    }
    //编写传感器事件监听器，该监听器继承自 SensorEventListener，并实现 onSensorChanged 和 onAccuracyChanged 两个方法
    @Override
        public void onSensorChanged (SensorEvent event) {
        float[] values = event.values; //获取 X、Y、Z 三轴的输出信息
        int sensorType = event.sensor.getType();//获取传感器类型
        StringBuilder stringBuilder = null;
        switch (sensorType) {
            case Sensor.TYPE_GRAVITY:
                    stringBuilder = new StringBuilder();
                    stringBuilder.append("X 轴横向重力值:");
                    stringBuilder.append(values[0]);
                    stringBuilder.append("\nY 轴纵向重力值:");
                    stringBuilder.append(values[1]);
                    stringBuilder.append("\nZ 轴向上重力值:");
                    stringBuilder.append(values[2]);
                    gravityEdt.setText(stringBuilder.toString());
                    break;
            case Sensor.TYPE_LIGHT:
                    stringBuilder = new StringBuilder();
                    stringBuilder.append("光的强度值:");
                    stringBuilder.append(values[0]);
                    lightEdt.setText(stringBuilder.toString());
                    break;
            }
    }
    //这个方法也是实现 SensorEventListener 接口时必须实现的方法
    @Override
            public void onAccuracyChanged (Sensor sensor,int accuracy){

            }
}


